package caiziyou.com.juc.volatiles;

public class SingletonDemo {

	private static volatile SingletonDemo instance = null;

	private SingletonDemo() {
		System.out.println(Thread.currentThread().getName() + "\t create");
	}

	// DCL (Double Check Lock 双端检索机制)
	public static SingletonDemo getInstance() {
		if (instance == null) {
			synchronized (SingletonDemo.class) {
				if (instance == null) {
					instance = new SingletonDemo();
				}
			}
		}
		return instance;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10000; i++) {
			new Thread(() -> {
				SingletonDemo.getInstance();
			}, String.valueOf(i)).start();
		}
		System.out.println("main stop");
	}
}
